package utils

import (
	"context"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
	"alertmanagerWebhook/global"
)

func HandleShutdown(srv *http.Server) {
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	global.Logger.Info("Shutting down server...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := srv.Shutdown(ctx); err != nil {
		global.Logger.Fatal("Server forced to shutdown:", err)
	}

	global.Logger.Info("Server exiting")
}